package com.lange.fileManagement.utils;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class AESUtil {
    private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final String ENCODING = "UTF-8";

    public static String decrypt(String data, String key, String iv) throws Exception {
        byte[] encryptedBytes = Base64.decodeBase64(data.getBytes(ENCODING));
        byte[] keyBytes = key.getBytes(ENCODING);
        byte[] ivBytes = iv.getBytes(ENCODING);

        SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(ivBytes));
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

        return new String(decryptedBytes, ENCODING);
    }
}